/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package cn.mabach.exception;

import cn.mabach.common.utils.R;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.connection.PoolException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestControllerAdvice
public class RRExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(RRException.class)
	public R handleRRException(RRException e, HttpServletRequest request){
		R r = new R();
		r.put("code", e.getCode());
		r.put("msg", e.getMessage());
		logger.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return r;
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public R handlerNoFoundException(Exception e,HttpServletRequest request) {
//		logger.error(e.getMessage(), e);
		logger.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error(404, "路径不存在，请检查路径是否正确");
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public R handleDuplicateKeyException(DuplicateKeyException e,HttpServletRequest request){
//		logger.error(e.getMessage(), e);
		logger.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("数据库中已存在该记录");
	}

	@ExceptionHandler(DataIntegrityViolationException.class)
	public R handleDataIntegrityViolationException(DataIntegrityViolationException e,HttpServletRequest request) {
//		logger.error(e.getMessage(), e);
		logger.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("字段太长,超出数据库字段的长度");
	}

	@ExceptionHandler(AuthorizationException.class)
	public R handleAuthorizationException(AuthorizationException e,HttpServletRequest request){
//		logger.error(e.getMessage(), e);
		logger.info("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("没有权限，请联系管理员授权");
	}

	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public R HttpRequestMethodNotSupportedException(Exception e,HttpServletRequest request){
//		logger.error(e.getMessage(), e);
		logger.info("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("没有权限，请联系管理员授权");
	}

	@ExceptionHandler(MaxUploadSizeExceededException.class)
	public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e,HttpServletRequest request) {
//		logger.error(e.getMessage(), e);
		logger.info("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("文件大小超出10MB限制, 请压缩或降低文件质量! ");
	}



	@ExceptionHandler(PoolException.class)
	public R handlePoolException(PoolException e,HttpServletRequest request) {
//		logger.error(e.getMessage(), e);
		logger.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("Redis 连接异常!");
	}


	@ExceptionHandler(Exception.class)
	public R handleException(Exception e,HttpServletRequest request){
		logger.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
		return R.error("操作失败");
	}
}
